<?php

	// +----------------------------------------------------------------------
	// | 留言模型
	// +----------------------------------------------------------------------
	// | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
	// +----------------------------------------------------------------------
	// | Author: 微尘 <yicmf@qq.com>
	// +----------------------------------------------------------------------

	namespace app\model\cms;

	use think\facade\Event;

	class LeaveMessage extends Common
	{

		public static $status_text = [
			-2 => '删除',
			-1 => '禁用',
			1 => '待回复',
			2 => '已回复',
		];

		public $send_info_text = [
			'1' => '不发送',
			'2' => '发送',
		];

		public $sex_list = [
			'0' => '保密',
			'1' => '男',
			'2' => '女'
		];

		/**
		 * 回复留言
		 * @param array $data
		 * @return bool
		 */
		public function reply($data = [])
		{
			if (empty($data) && !$data['id']) {
				$this->error = Lang::get('__data_format_error_');
				return false;
			}
			$message = $this->get($data['id']);
			if (!$message || !is_object($message)) {
				$this->error = '留言不存在或者已经删除';
				return false;
			}
			if ($message->status >= 2) {
				$this->error = '该留言已经回复';
				return false;
			}
			$result = $message->save($data);
			if (!$result) {
				$this->error = $message->getError();
				return false;
			}
			Event::trigger('reply_user_message', $data);
			return $result;
		}

		//获取器
		public function getSendInfoTextAttr($value, $data)
		{
			return $this->send_info_text[$data['send_info']];
		}

		public function getSexAttr($value)
		{
			return $this->sex_list[$value];
		}


		// 类型转换
		protected $type = [
		];

		/**
		 * 写入修改
		 * @param $data
		 */
		public static function onBeforeUpdate($data)
		{
			$data['reply_time'] = time_format();
			$data['admin_id'] = '';
		}
		/**
		 * 写入修改
		 * @param $data
		 */
		public static function onBeforeInsert($data)
		{
			$data['status'] = 0;
			$data['is_show'] = 0;
			$data['ip'] = self::$request->ip();
		}


	}
